<template>
  <div class="packingList">
    <div class="publicSearch">
      <div class="searchBar" :style="{ height: isMoreSearch ? 'auto' : '' }">
        <el-button-group>
          <el-button @click="getpackingListData" size="mini" icon="el-icon-search">查询</el-button>
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh">重置</el-button>
        </el-button-group>
        <div>
          <span>批次编码查询</span>
          <el-input v-model="searchData.batchName" size="mini" placeholder="批次编码" clearable></el-input>
        </div>

        <div>
          <span>库位查询</span>
          <el-input v-model="searchData.fromCellCode" size="mini" placeholder="库位" clearable></el-input>
        </div>

        <div>
          <span>容器查询</span>
          <el-input v-model="searchData.toBoxCode" size="mini" placeholder="容器" clearable></el-input>
        </div>

        <div>
          <span>入库单号查询</span>
          <el-input v-model="searchData.transferBillCode" size="mini" placeholder="入库单号" clearable></el-input>
        </div>
      </div>
    </div>

    <div class="headerBtns">
      <el-button
        size="small"
        type="primary"
        round
        icon="iconfont icon-xinzeng"
        @click="dialogNewVisible = true"
      >新增</el-button>
    </div>
    <!--新增装箱单-->
    <el-dialog
      title="新增装箱单"
      :visible.sync="dialogNewVisible"
      :before-close="handleClose"
      width="500px"
      :close-on-click-modal="false"
    >
      <div class="headerBtns">
        <el-button
          size="small"
          type="primary"
          round
          icon="iconfont icon-xinzeng"
          @click="showSelectAcceptRecord"
        >选择收货单</el-button>
      </div>

      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="物料编码" prop="itemCode" :label-width="formLabelWidth">
          <el-input
            v-model="form.itemCode"
            placeholder="请输入物料编码"
            maxlength="20"
            style="width: 90%;"
            :disabled="true"
          ></el-input>
        </el-form-item>

        <el-form-item label="物料名称" prop="itemName" :label-width="formLabelWidth">
          <el-input
            v-model="form.itemName"
            placeholder="请输入物料名称"
            style="width: 90%;"
            :disabled="true"
          ></el-input>
        </el-form-item>

        <el-form-item label="库位" prop="fromCellCode" :label-width="formLabelWidth">
          <el-input
            v-model="form.fromCellCode"
            placeholder="请输入库位"
            style="width: 90%;"
            :disabled="true"
          ></el-input>
        </el-form-item>

        <el-form-item label="批次号" prop="batchName" :label-width="formLabelWidth">
          <el-input
            v-model="form.batchName"
            placeholder="请输入批次号"
            style="width: 90%;"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="总数量" prop="quantity" :label-width="formLabelWidth">
          <el-input
            v-model="form.quantity"
            placeholder="请输入装箱数量"
            style="width: 90%;"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="装箱数量" prop="qty" :label-width="formLabelWidth">
          <el-input v-model="form.qty" placeholder="请输入装箱数量" style="width: 90%;"></el-input>
        </el-form-item>

        <el-form-item label="容器号" prop="toBoxCode" :label-width="formLabelWidth">
          <el-input v-model="form.toBoxCode" placeholder="请输入容器号" style="width: 90%;"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form');dialogNewVisible = false">取 消</el-button>
        <el-button type="primary" @click="commitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 选择收货单-->
    <el-dialog
      width="800px"
      title="选择收货单"
      :visible.sync="innerVisibleInventory"
      append-to-body
      style="margin-top: -10vh"
    >
      <div class="publicSearch body">
        <div class="searchBar" :style="{ height: isMoreSearch2 ? 'auto' : '' }">
          <el-button-group>
            <el-button @click="getAcceptRecordData" size="mini" icon="el-icon-search">查询</el-button>
            <el-button @click="clearsearchSelectAcceptRecord" size="mini" icon="el-icon-refresh">重置</el-button>
          </el-button-group>
          <div>
            <span>入库单明细号</span>
            <el-input
              v-model="searchSelectAcceptRecord.asnDetailNo"
              size="mini"
              placeholder="入库单明细号"
              clearable
            ></el-input>
          </div>
          <div>
            <span>物料名称</span>
            <el-input
              v-model="searchSelectAcceptRecord.itemName"
              size="mini"
              placeholder="物料名称"
              clearable
            ></el-input>
          </div>
          <div>
            <span>物料编码</span>
            <el-input
              v-model="searchSelectAcceptRecord.itemCode"
              size="mini"
              placeholder="物料编码"
              clearable
            ></el-input>
          </div>
          <div>
            <span>关键字搜索</span>
            <el-input
              v-model="searchData.keyWords"
              size="mini"
              title="批次名称/验收人/追踪号/货位编码/包装"
              placeholder="批次名称/验收人/追踪号/货位编码/包装"
              clearable
            ></el-input>
          </div>
        </div>
        <span
          class="moreSearchBtn"
          :class="isMoreSearch2 ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
          @click="isMoreSearch2 = !isMoreSearch2"
        ></span>
      </div>

      <div class="tableBox">
        <el-table
          max-height="460"
          :data="acceptRecords"
          @selection-change="handleSelectionChange"
          style="width: 100%"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column fixed="left" type="index" width="50"></el-table-column>
          <el-table-column
            sortable
            show-overflow-tooltip
            prop="acceptRecordCode"
            label="收货单号"
            width="150"
          ></el-table-column>
          <el-table-column sortable show-overflow-tooltip prop="state" label="状态" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0" style="color: #409eff">初始化</span>
              <span v-if="scope.row.state == 2" style="color: #67c23a">已确认</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="qcState" label="质检状态" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.qcState == 0" style="color: #409eff">免检</span>
              <span v-if="scope.row.qcState == 1" style="color: #409eff">待检</span>
              <span v-if="scope.row.qcState == 2" style="color: #67c23a">已检</span>
            </template>
          </el-table-column>
          <el-table-column
            sortable
            show-overflow-tooltip
            prop="organizationName"
            label="货主"
            width="120"
          >
            <template slot-scope="scope">
              <span
                class="textBtn"
                @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.organizationId,
                  'organization'
                )
              "
              >{{ scope.row.organizationName }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable show-overflow-tooltip prop="itemName" label="物料名称" width="160">
            <template slot-scope="scope">
              <span
                class="textBtn"
                @click="
                $EventBus.$emit('showPublicModal', scope.row.itemCode, 'item')
              "
              >{{ scope.row.itemName }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable show-overflow-tooltip prop="itemCode" label="物料编码" width="160">
            <template slot-scope="scope">
              <span
                class="textBtn"
                @click="
                $EventBus.$emit('showPublicModal', scope.row.itemCode, 'item')
              "
              >{{ scope.row.itemCode }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable show-overflow-tooltip prop="batchName" label="批次" width="100"></el-table-column>
          <el-table-column show-overflow-tooltip prop="quantity" label="数量" width="80"></el-table-column>

          <el-table-column sortable show-overflow-tooltip prop="cellCode" label="货位编码" width="120"></el-table-column>

          <el-table-column sortable show-overflow-tooltip prop="boxCode" label="追踪号" width="120"></el-table-column>
          <el-table-column
            sortable
            show-overflow-tooltip
            prop="acceptUserName"
            label="验收人"
            width="100"
          >
            <template slot-scope="scope">
              <span
                class="textBtn"
                @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.acceptUserId,
                  'account'
                )
              "
                type="text"
              >{{ scope.row.acceptUserName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            sortable
            show-overflow-tooltip
            prop="acceptTime"
            label="收货时间"
            width="120"
          >
            <template slot-scope="scope">{{ scope.row.acceptTime.substr(5, 11) }}</template>
          </el-table-column>

          <el-table-column
            sortable
            show-overflow-tooltip
            prop="asnDetailNo"
            label="入库单明细号"
            width="130"
          >
            <template slot-scope="scope">
              <span
                class="textBtn"
                @click="
                $router.push({
                  name: 'asnBillDetail',
                  params: { billNo: scope.row.asnBillNo },
                })
              "
              >{{ scope.row.asnDetailNo }}</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: right"
          @current-change="handleCurrentChange2"
          :current-page.sync="searchSelectAcceptRecord.pageNum"
          :page-size="searchSelectAcceptRecord.pageSize"
          layout="total, prev, pager, next, jumper"
          :total="searchSelectAcceptRecord.total"
        ></el-pagination>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="innerVisibleInventory = false">取 消</el-button>
        <el-button type="primary" @click="clickSelectInventory">确 定</el-button>
      </div>
    </el-dialog>

    <div class="tableBox none">
      <el-table
        v-loading="loading.masterTable"
        highlight-current-row
        :data="packingLists"
        style="width: 100%"
        :row-style="Mystyle"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column fixed="left" type="index" width="50"></el-table-column>

        <el-table-column show-overflow-tooltip prop="transferBillCode" label="单据号" min-width="150">
          <template slot-scope="scope">
            <span class="textBtn" @click="test(scope.row.transferBillCode, 'item')">
              {{
              scope.row.transferBillCode
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="toBoxCode"
          label="容器编码"
          min-width="150"
        ></el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromCellCode"
          label="库位编码"
          min-width="150"
        ></el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="batchName"
          label="批次号"
          min-width="150"
        ></el-table-column>
        <el-table-column show-overflow-tooltip prop="itemCode" label="物料编码" min-width="150">
          <template slot-scope="scope">
            <span class="textBtn" @click="test(scope.row.itemCode, 'item')">
              {{
              scope.row.itemCode
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="itemName" label="物料名称" min-width="150">
          <template slot-scope="scope">
            <span class="textBtn" @click="test(scope.row.itemName, 'item')">
              {{
              scope.row.itemName
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column sortable show-overflow-tooltip prop="qty" label="数量" min-width="150"></el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="wareName"
          label="仓库名称"
          min-width="150"
        ></el-table-column>
        <el-table-column show-overflow-tooltip prop="confirmUserName" label="操作员" min-width="150">
          <template slot-scope="scope">
            <span class="textBtn" @click="test(scope.row.confirmUserName, 'item')">
              {{
              scope.row.confirmUserName
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="confirmTime"
          label="装箱时间"
          min-width="150"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="80">
          <template slot-scope="scope">
            <el-button
              @click="showNewModal(scope.row)"
              type="primary"
              size="small"
              :disabled="scope.row.state === 2"
            >&nbsp;确认</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right"
        class="pagination-bottom"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :page-sizes="[10, 20, 30, 50]"
        :current-page.sync="searchData.pageNum"
        :page-size="searchData.pageSize"
        layout="sizes,total, prev, pager, next, jumper"
        :total="searchData.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "packingList",
  data() {
    // 校验规则
    return {
      isMoreSearch: false,
      loading: {
        masterTable: true
      },
      // 用作查询的数据
      searchData: {
        // 仓库编码 获得当前仓库的编码
        wareId: PF.getLocal("wareId", "number"),
        pageNum: 1,
        pageSize: 10,
        total: null,
        // 批次号查询
        batchName: "",
        // 单据号
        transferBillCode: "",
        // 库位
        fromCellCode: "",
        // 容器号
        toBoxCode: ""
      },
      searchStates: [
        {
          value: "enable",
          label: "启用"
        },
        {
          value: "disable",
          label: "禁用"
        }
      ],
      //存放装箱单数据的数组
      packingLists: [],
      // 存放收货单的数据

      // 控制新增的页面
      dialogNewVisible: false,
      // 控制选择收货单页面
      innerVisibleInventory: false,
      form: {
        // 仓库编码 获得当前仓库的编码
        wareId: PF.getLocal("wareId", "number"),
        // 单据号
        transferBillCode: "",
        // 物料编码
        itemCode: "",
        // 物料名称
        itemName: "",
        // 库位
        fromCellCode: "",
        // 容器号
        toBoxCode: "",
        // 总数量
        quantity: "",
        // 批次号
        batchName: "",
        // 装箱数量
        qty: "",
        // 包装编码
        packDetailId: "",
        // 冻结标识
        freezeFlag: ""
      },
      // 查询收货单的条件
      searchSelectAcceptRecord: {
        pageNum: 1,
        pageSize: 5,
        total: null,
        keyWords: "",
        wareId: PF.getLocal("wareId", "number"),
        organizationId: null,
        asnDetailNo: "",
        itemName: "",
        itemCode: ""
      },
      // 控制
      isMoreSearch2: false,
      // 存放收货单的数据集合
      acceptRecords: [],
      // 新增转移单的表单校验
      rules: {
        transferBillCode: [
          { required: true, message: "请输入单据号", trigger: "blur" }
        ],
        itemCode: [
          { required: true, message: "请输入物料编码", trigger: "blur" }
        ],
        itemName: [
          { required: true, message: "请输入物料名称", trigger: "blur" }
        ],
        cellCode: [
          { required: true, message: "请输入目标库位", trigger: "blur" }
        ],
        toBoxCode: [
          { required: true, message: "请输入容器号", trigger: "blur" }
        ],
        batchName: [
          { required: true, message: "请输入批次号", trigger: "blur" }
        ],
        fromCellCode: [
          { required: true, message: "请输入来源库位", trigger: "blur" }
        ],
        qty: [{ required: true, message: "请输入数量", trigger: "blur" }]
      },
      formLabelWidth: "120px",
      rowData: {
        //
      },
      Mystyle: { height: "50px" },
      filterText: "",
      defaultProps: {
        children: "children",
        label: "name"
      },
      roleRowInfo: {
        //
      },
      roleForm: {
        //
      },
      roleRules: {
        //
      },
      tableRowClassName({ row, rowIndex }) {
        if (row.accountStatus === "disable") {
          return "disable-row";
        } else if (row.accountStatus === "enable") {
          return "enable-row";
        }
        return "";
      },
      // 选中的数据
      multipleSelection: [],
      // 用于装箱单确认的数据
      updateData: {
        // 仓库编码 获得当前仓库的编码
        wareId: PF.getLocal("wareId", "number"),
        // 单据号
        transferBillCode: "",
        // 单据类型为装箱单
        billType: 0
      }
    };
  },
  computed: {},
  watch: {
    //
  },
  methods: {
    //分页设置每页的条数
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getpackingListData();
    },
    //分页设置从第几条开始
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getpackingListData();
    },
    // 清空查询条件批次号
    clearSearchData() {
      this.searchData.batchName = "";
    },
    //根据筛选条件查询数据
    getpackingListData() {
      let that = this;
      that.loading.masterTable = true;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      IOT.getServerData("/transfer/packwork/select", "get", searchData, ret => {
        that.loading.masterTable = false;
        if (ret.code === 200) {
          // rows就是返回的数据
          this.packingLists = ret.rows;
          this.searchData.total = ret.total;
          this.searchData.pageNum = ret.pageNumber;
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },
    // 新增的重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭转移单新增
    handleClose() {
      // this.$confirm('确认关闭？')
      //     .then(_ => {
      this.resetForm("form");
      this.dialogNewVisible = false;
      //     done();
      // })
      // .catch(_ => {});
    },
    // 提交表单--新增装箱单
    commitForm(formName) {
      let that = this;
      IOT.showOverlay("保存中，请稍等...");
      this.$refs[formName].validate(valid => {
        if (valid) {
          IOT.getServerData(
            "/transfer/PackWork/insert",
            "post",
            this.form,
            ret => {
              if (ret.code === 200) {
                IOT.hideOverlay();
                IOT.tips("新增成功", "success", 1000, () => {
                  IOT.showOverlay("正在刷新，请稍等...");
                  // 刷新当前页面
                  this.getpackingListData();
                  IOT.hideOverlay();
                });
              } else {
                IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
                IOT.hideOverlay();
              }
            },
            true
          );
          this.resetForm("form");
          this.dialogNewVisible = false;
        } else {
          IOT.hideOverlay();
          console.error("error submit!!");
          return false;
        }
      });
    },

    //查询收货单数据
    showSelectAcceptRecord() {
      //查询收货单
      this.getAcceptRecordData();
      // 打开弹框
      this.innerVisibleInventory = true;
    },
    // 选择收货单的查询条件的重置
    clearsearchSelectAcceptRecord() {
      this.searchSelectAcceptRecord.keyWords = "";
      this.searchSelectAcceptRecord.itemName = "";
      this.searchSelectAcceptRecord.itemCode = "";
      this.searchSelectAcceptRecord.asnDetailNo = "";
    },
    // 获取收货单的数据
    getAcceptRecordData() {
      var that = this;
      that.acceptRecords = [];
      that.loading.masterTable = true;
      IOT.getServerData("/accept/records/list", "get", that.searchData, ret => {
        that.loading.masterTable = false;
        if (ret.code === 200) {
          that.acceptRecords = ret.rows;
          that.searchSelectAcceptRecord.total = ret.total;
          that.searchSelectAcceptRecord.pageNum = ret.pageNumber;
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },

    // 选择收货单数据时
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 确定后带回数据
    clickSelectInventory() {
      // 获取待回的数据
      let list = PF.JSON(this.multipleSelection);
      // 物料编码
      this.form.itemCode = list[0].itemCode;
      // 物料名称
      this.form.itemName = list[0].itemName;
      // 来源库位
      this.form.fromCellCode = list[0].cellCode;
      // 批次号
      this.form.batchName = list[0].batchName;
      // 总数量
      this.form.quantity = list[0].quantity;
      // 包装编码
      this.form.packDetailId = list[0].packDetailId;
      // 冻结标识
      if (list[0].qcState == "1") {
        //说明是待检的物料
        this.form.freezeFlag = 1;
      }
      // 关闭弹框
      this.innerVisibleInventory = false;
    },

    // 查询收货单的分页
    handleCurrentChange2(val) {
      this.searchSelectAcceptRecord.pageNum = val;
      this.getAcceptRecordData();
    },
    //装箱单确认
    showNewModal(row) {
      if (row == null) {
        this.$message({
          message: "请选择一条数据",
          type: "warning"
        });
      } else {
        // 这里来确认转移单
        let that = this;
        that.loading.masterTable = true;
        this.updateData.transferBillCode = row.transferBillCode;
        let updateData = JSON.parse(JSON.stringify(this.updateData));
        IOT.getServerData(
          "/transfer/transferBill/updateState",
          "post",
          updateData,
          ret => {
            that.loading.masterTable = false;
            if (ret.code === 200) {
              IOT.tips(ret.message || "操作成功", "success");
              // rows就是返回的数据
              this.getpackingListData();
            } else {
              IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
            }
          }
        );
      }
    }
    // // 进行删除
    // isDelete(id) {
    //   this.$confirm("确认删除？")
    //     .then(_ => {
    //       this.delete(id);
    //     })
    //     .catch(_ => {});
    // },
    // // 根据主键id删除
    // delete(id) {
    //   let that = this;
    //   IOT.showOverlay("删除中，请稍等...");
    //   IOT.getServerData("/packingList/deleteById", "get", { id: id }, ret => {
    //     IOT.hideOverlay();
    //     if (ret.code === 200) {
    //       IOT.tips("删除成功！", "success", 1000, () => {
    //         that.getpackingListData();
    //       });
    //     } else {
    //       IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
    //     }
    //   });
    // },
  },
  // 页面加载时刷新
  activated() {
    this.searchData.pageNum = 1;
    this.searchData.pageSize = 10;
    //请求数据
    this.getpackingListData();
  }
};
</script>

<style scoped lang="less">
.packingList {
}
</style>
